//
//  ALBUME.m
//  Music box
//
//  Created by Rainseylong on 10/21/12.
//  Copyright (c) 2012 iMer. All rights reserved.
//

#import "ALBUME.h"
#import "Apiconnection.h"

static NSString *const SubAlbumURLmain =@"Album/albumJson/";
static NSString *const SubAlbumURLlast =@"/1000/1/g2cSBfct1uaGThCKWcHejter7eqlEXz3P14V7JewTao";

@implementation ALBUME

@synthesize albumeId= _albumeId;
@synthesize albumeNameRendered= _albumeNameRendered;
@synthesize productionId= _productionId;
@synthesize dateUpdated=_dateUpdated;
@synthesize dateCreated=_dateCreated;
@synthesize albumName=_albumeName;
@synthesize albumUrl=_albumeUrl;

- (id)initWithAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
        
    _albumeName = [attributes valueForKey:@"albumName"];
    _albumeNameRendered = [attributes valueForKey:@"albumNameRendered"];
    _productionId = [attributes valueForKey:@"productionId"];
    _dateUpdated = [attributes valueForKey:@"dateUpdated"];
    _dateCreated = [attributes valueForKey:@"dateCreated"];
    _albumeId=[attributes valueForKey:@"albumId"];
    _albumeUrl=[attributes valueForKey:@"url"];

    return self;
}

+ (void)convertAlbumeToOBjectWithBlock:(void (^)(NSArray *albumArray, NSError *error))block
                         withProductID:(int)productionID
{
    NSString *url_album=[NSString stringWithFormat:@"%@%d%@",SubAlbumURLmain,productionID,SubAlbumURLlast];
    [[Apiconnection sharedClient] getPath:url_album parameters:Nil success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSMutableArray *tempDic=[responseObject valueForKey:@"responseBody"];
        NSMutableArray *mutablePosts = [NSMutableArray arrayWithCapacity:tempDic.count];
    
        for (int i=0;i < tempDic.count;i++) {
        
            ALBUME *post = [[ALBUME alloc] initWithAttributes:[tempDic objectAtIndex:i]];
            [mutablePosts addObject:post];
        }
        
        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        if (block) {
            block([NSArray array], error);
        }
    }];
}



@end
